﻿//=====================================================
// Copyright © 2017-2018 xuxuzhaozhao
// All rights reserved
// GUID: 4858c268-4010-47ba-89be-2ef8802ca5f0
// CLR版本: 4.0.30319.42000
// 命名空间名称: xuxuzhaozhao.Blog.Web.Authorize
// 文件名: XuPrincipal
// 当前系统时间: 2017/11/7 16:15:17
// 当前登录用户名: xuxuzhaozhao
//======================================================

using System;
using System.Linq;
using System.Security.Principal;
using xuxuzhaozhao.Blog.Infrastructure.XuEntityFramework;

namespace xuxuzhaozhao.Blog.XuAuthorize
{
    public class XuUser : IPrincipal
    {
        public XuUser()
        {
        }
        /// <summary>  
        /// 构造函数，用户名构造  
        /// </summary>  
        /// <param name="userName"></param>  
        public XuUser(string userName)
        {
            //根据UserName查询数据库获得以下数据
            using (var db = new XuxuzhaozhaoDbContext())
            {
                var user = db.XuUser.SingleOrDefault(t => t.UserName == userName);
                if (user == null) throw new Exception($"没有该用户，账号:{userName}");
                this.UserName = user.UserName;
                this.Password = user.Password;
                this.Phone = user.Phone;
                this.RealName = user.RealName;
            }
        }

        /// <summary>  
        /// 获取当前用户标识  
        /// </summary>  
        public IIdentity Identity { get; }

        /// <summary>  
        /// 当前用户是否指定角色（采用权限值方式，此处返回false）  
        /// </summary>  
        /// <param name="role"></param>  
        /// <returns></returns>  
        public bool IsInRole(string role) => false;     

        /// <summary>
        /// 用户账号
        /// </summary>
        public string UserName { get; }

        /// <summary>
        /// 用户所在部门
        /// </summary>
        public string Password { get; }

        /// <summary>
        /// 用户电话
        /// </summary>
        public string Phone { get; }

        /// <summary>
        /// 真实姓名
        /// </summary>
        public string RealName { get; }
    }
}